<template>
  <div>
    <BreakSpace label="基本信息" :containerStyle="{ marginBottom: '10px' }"/>
    <FormPanel
      ref="baseInfo"
      :list="initFormBase"
      :cols="2"
      :labelWidth="100"
      :isSubmitBtn="false"
      :formType="handleType"
    />
    <BaseDialog :visible.sync="visbleDealer" title="选择回收网点"
                :containerStyle="{ overflow: 'auto', paddingBottom: '60px' }"
                destroyOnClose>
      <add-dealer
        selectionType="single"
        @selectArr="selectDealerFun"
        @closeDialog='visbleDealer = false'></add-dealer>
    </BaseDialog>
    <div class="dialogBottmView">
      <el-button @click="closeDialogTop">返 回</el-button>
      <el-button v-if="handleType != 'show'" @click="dosave()" :loading="saveLoading" type="primary">
        保存
      </el-button>
    </div>
  </div>
</template>
<script>
import {dictionary} from '@/utils/dictMixin';
import {doSave} from '@/api/partsmanage/batteryTrace/branchEdit';
import {sfqyArr} from './constant';
import addDealer from "./addDealer";


export default {
  components: {addDealer},
  props: {
    handleType: {
      type: String,
      default: 'add'
    },
    value: {
      type: Object,
      default: null
    }
  },
  mixins: [dictionary],
  data() {
    return {
      visbleDealer:false,
      formData: {},
      initFormBase: this.initFormBaseFun(),
      saveLoading: false,
    };
  },
  mounted() {
    //判断是新增，还是修改
    if (this.handleType == 'add') {
    } else {
      this.formData = Object.assign({}, this.value.data);
      this.init();
    }
  },
  computed: {
    culFormData() {
      return this.formData
    }
  },
  methods: {
    /*选择经销商信息回调 */
    selectDealerFun(arr) {
      const dealer = arr[0]
      this.visbleDealer = false;
      this.initFormBase.find(i => i.fieldName == 'code').initialValue = dealer.dealerCode;
      this.initFormBase.find(i => i.fieldName == 'name').initialValue = dealer.dealerName;
    },
    //修改时，初始化页面数据
    init() {
      //循环为表单赋值
      this.initFormBase.forEach(x => {
        x.initialValue = this.culFormData[x.fieldName];
      });
    },
    //基本表单信息
    initFormBaseFun() {
      return [
        {
          type: 'INPUT',
          label: '回收网点代码',
          fieldName: 'code',
          disabled: true,
          placeholder: '请输入',
          hidden:false,
          initialValue: '',
          rules: [
            {required: true, trigger: 'change', message: '回收网点代码不能为空'}
          ],
        },
        {
          type: 'INPUT',
          label: '回收网点名称',
          fieldName: 'name',
          placeholder: '请选择',
          readonly: true,
          initialValue: '',
          rules: [
            {required: true, trigger: 'change', message: '回收网点名称不能为空'}
          ],
          unitRender: () => {
            return (
              <div>
              <el-button icon="el-icon-search" onClick={() => this.visbleDealer = true}></el-button>
            </div>
          )
          }
        },
        {
          type: 'INPUT',
          label: '社会信用代码',
          fieldName: 'shxycode',
          placeholder: '请输入',
          initialValue: '',
          rules: [
            {required: true, message: '社会信用代码不能为空', trigger: 'change'},
          ]
        },
        {
          type: 'SELECT',
          label: '是否启用',
          fieldName: 'xtqybz',
          placeholder: '请选择',
          initialValue: true,
          itemList: sfqyArr,
          rules: [
            {required: true, message: '请选择', trigger: 'change'},
          ]
        }
      ];
    },
    //保存
    async dosave() {
      this.saveLoading = true;
      let [err, data] = await this.$refs.baseInfo.GET_FORM_DATA();
      if (err) {
      } else {
        this.formData = Object.assign({}, this.formData, data);
        let res= await doSave(this.formData);
        if (res.resultCode === 200) {
          this.$notify.success({title: '提示信息', message: '保存成功!'});
          // 重新加载
          this.closeDialog('refresh');
        }
      }
      this.saveLoading = false;
    },
    closeDialogTop() {
      return this.closeDialog('closeDialog');
    },
    //取消操作
    closeDialog(type) {
      this.$emit('close', type);
    }
  }
};
</script>

<style lang="less" scoped>
</style>
